

//
//  PhotoBrowserViewController.swift
//  PhotoBrowser
//
//  Created by 林彬 on 16/4/28.
//  Copyright © 2016年 linbin. All rights reserved.
//

import UIKit

class PhotoBrowserViewController: UIViewController {
    
    private let ID = "imageCellID"
    // 定义一个模型数组和indexPath,来接受主界面控制器传递过来的参数
    var shopArr : [Shop]?
    var indexPath :NSIndexPath?
    
    var indexPathCallBack : ((imageVIndexPath : NSIndexPath) -> ())?
    
    
    // MARK:- 懒加载子控制器
    // 控制器的View上有三个子控件:2个按钮,1个collectionView.懒加载
    private lazy var closeButton : UIButton = UIButton(title: "关  闭", fontSize: 14, bgColor: UIColor.darkGrayColor())
    private lazy var saveButton : UIButton = UIButton(title: "保  存", fontSize: 14, bgColor: UIColor.darkGrayColor())
    private lazy var imageCollectionView : UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: PhotoBrowserViewFlowLayout())

    
    override func loadView() {
        super.loadView()
        // 给View的宽度加15,为了拖动的时候显示15的间距
        // 但是要在下面设置图片对齐方式,免得每次滑动后,图片都会错位15
        view.frame.size.width += 15
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.blackColor()
        // MARK:- 设置UI
        setUpUi()
        
        // 就是在这里设置对齐方式.顺便滚到对应的位置
        imageCollectionView.scrollToItemAtIndexPath(indexPath!, atScrollPosition: .Left, animated: false)
    }
  
}

// MARK:- 实现dimissDelegat中的方法
extension PhotoBrowserViewController : DismissDelegate {
    func currentIndexPath() -> NSIndexPath {
        // 1.获取正在显示的cell
        let cell = imageCollectionView.visibleCells().first!
        
        // 2.获取indexPath
        return imageCollectionView.indexPathForCell(cell)!
    }
    
    func imageView() -> UIImageView {
        // 1.创建UIImageView对象
        let imageView = UIImageView()
        
        // 2.设置对象的图片
        // 2.1.获取正在显示的cell
        let cell = imageCollectionView.visibleCells().first as! PhotoBrowserViewCell
        
        // 2.2.取出图片
        imageView.image = cell.imageV.image
        
        // 3.设置imageView的属性
        imageView.contentMode = .ScaleAspectFill
        imageView.clipsToBounds = true
        
        return imageView
    }
}

// MARK:- 设置子控件
extension PhotoBrowserViewController{
    func setUpUi() {
        // 添加子控件
        view.addSubview(imageCollectionView)
        view.addSubview(saveButton)
        view.addSubview(closeButton)
        
        
        // 设置子控件frame
        let btnW : CGFloat = 93
        let btnH : CGFloat = 32
        let btnY :CGFloat = UIScreen.mainScreen().bounds.size.height - btnH - 20
        closeButton.frame = CGRect(x: 20, y: btnY, width: btnW, height: btnH)
        let saveBtnX : CGFloat = UIScreen.mainScreen().bounds.size.width - 20 - btnW
        saveButton.frame = CGRect(x: saveBtnX, y: btnY, width: btnW, height: btnH)
        
        
        imageCollectionView.frame = view.bounds
        imageCollectionView.registerClass(PhotoBrowserViewCell.self, forCellWithReuseIdentifier: ID)
        imageCollectionView.delegate = self
        imageCollectionView.dataSource = self
        
        closeButton.addTarget(self, action: #selector(PhotoBrowserViewController.closeBtnClick), forControlEvents: .TouchUpInside)
        saveButton.addTarget(self, action: #selector(PhotoBrowserViewController.saveBtnClick), forControlEvents: .TouchUpInside)
        
    }
}

extension PhotoBrowserViewController : UICollectionViewDataSource , UICollectionViewDelegate{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // ?? 一般用在可选链后面.要有空格分开
        // 如果前面的可选类型有一个没有值,那么直接使用??后面的值
        // 如果前面的可选类型都有值,那么对最终的解决进行解包
        
        return shopArr?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ID, forIndexPath: indexPath) as! PhotoBrowserViewCell
        
        cell.shop = shopArr?[indexPath.item]

        // 把indexPath放到闭包参数中传递给HomeViewController
        self.indexPathCallBack!(imageVIndexPath: indexPath)
        
       
        return cell
    }
    
    
    func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        // 点击cell,也就是点击图片,就相当于dissmiss了当前控制器.
        closeBtnClick()
    }
    
    
}

// MARK:- 监听按钮点击事件
extension PhotoBrowserViewController{
    @objc func closeBtnClick() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    @objc func saveBtnClick() {
        // 1.取出正在屏幕中显示的Cell
        let cell = imageCollectionView.visibleCells().first as! PhotoBrowserViewCell
        
        // 2.取出image.由于要获得cell的imageV属性,所以,自定义的cell里的imageV属性不能是private
        let image = cell.imageV.image
        
        // 3.写入相册
        UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
    }
}

// MARK:- 供外界使用的闭包回调函数
extension PhotoBrowserViewController{
    func getIndexPath( callBack : ((imageVIndexPath : NSIndexPath) -> ()) ) {
        self.indexPathCallBack = callBack
    }
}

